package com.stx.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.stx.entity.Department;
import com.stx.jdbc.JdbcUtil;
import com.stx.jdbc.StringUtil;

/**
 * Servlet implementation class deditservlet
 */
public class deditservlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public deditservlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String dname=request.getParameter("dname");
		String daddress=request.getParameter("daddress");
		String dphone=request.getParameter("dphone");		
		String did=request.getParameter("did");
		System.out.println(dname+"1");
		System.out.println(daddress+"1");
		System.out.println(dphone+"1");
		System.out.println(did+"1");
		Connection con = null;
		PreparedStatement pst = null;
		List<Department> deplist = new ArrayList<Department>();
		try {
			con = JdbcUtil.getCon();
			//执行更新科室的操作
			pst = con.prepareStatement("update tbl_department set dname=?,daddress=?,dphone=? where did=?");
			pst.setString(1,dname);
			pst.setString(2,daddress);
			pst.setString(3,dphone);
			pst.setString(4,did);
			pst.executeUpdate();
			//获取所有科室的信息
			pst = con.prepareStatement("select did,dname,daddress,dphone from tbl_department");
			ResultSet rs = pst.executeQuery();
			//将查询出来的数据转放到List中，供传递
			while (rs.next()) {
				Department d = new Department();
				d.setdId(rs.getString("did"));
				d.setDname(rs.getString("dname"));
				d.setDaddress(rs.getString("daddress"));
				d.setDphone(rs.getString("dphone"));
				deplist.add(d);
			}
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		} finally {
			JdbcUtil.closeCon(con, pst);
		}
		request.setAttribute("deplist", deplist);
		request.getRequestDispatcher("/admin/department.jsp").forward(request, response);
	}

}
